<?php

/**
 * PassportController.class.php 
 * 
 *
 * @author     late.xiao@qq.com
 */
namespace Sysmanger\Controller;

use Base\Lib\BaseController;

class PassportController extends BaseController {

    public function __construct()
    {
        parent::__construct();
    }
    
    public function login()
    {
        $system_title = '登录后台';
        $act_url = U('Passport/doLogin');
        $this->assign ('system_title', $system_title);
        $this->assign('is_login', false);
        $this->assign('act_url', $act_url);
        $this->display();
    }
    
    public function doLogin()
    {
        $request = I('post.');
        $obj_serv = D('Passport', 'Service');
        try {
            $manger_info = array();
            $bool = $obj_serv->checkpwd($request, $manger_info);
            if($bool)
            {
                $this->_setSession($manger_info);
                $this->_setLoginLog();
                return $this->success('登录成功！', U('System/Index/index'));
            }
            
            return $this->error('登录失败，请重新登录');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
    
    public function logout()
    {
        session('manger', null);
        return $this->success('已退出登录!', U('Passport/login'));
    }
    
    // 设置登录SESSION
    protected function _setSession($manger_info)
    {
        $manger_info['expire'] = 7200;
        $manger_info['set_time'] = NOW_TIME;
        session('manger', $manger_info);
        
        return true;
    }
    
    // 设置登录日志
    protected function _setLoginLog()
    {
        $mid = session('manger.mid');
        if(!$mid)
        {
            return false;
        }
        
        $obj_serv = D('Passport', 'Service');
        $data['login_time'] = NOW_TIME;
        $data['login_ip'] = get_client_ip();
        
        $obj_serv->updateManger($mid, $data);
        
        return true;
    }
}


 